package com.hunlian.makelove.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hunlian.makelove.AppApplication;
import com.hunlian.makelove.R;
import com.hunlian.makelove.bean.PrivacySettingBean;
import com.hunlian.makelove.common.ActivityMgr;
import com.hunlian.makelove.common.AppConfig;
import com.hunlian.makelove.net.NetVisitCallback;
import com.hunlian.makelove.net.ServiceInterfaceDef;
import com.hunlian.makelove.net.api.PrivacySetServiceVisit;
import com.hunlian.makelove.tools.AesEncryptionUtil;
import com.hunlian.makelove.tools.MyToast;
import com.hunlian.makelove.view.CustomProgressDialog;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lenovo on 2016/3/15.
 */
public class SettingActivity extends BaseActivity {

    private SettingActivity mActivity;
    private String mTitle;
    private RelativeLayout mRlAlertPw;
    private RelativeLayout mHelp;
    private RelativeLayout mContact;
    private RelativeLayout mAbout;
    private RelativeLayout mFeedback;
    private Button mBtnExit;

    @BindView(R.id.rlPrivacy)
    RelativeLayout rlPrivacy;
    @BindView(R.id.vEdu)
    View vEdu;
    @BindView(R.id.rlEdu)
    RelativeLayout rlEdu;

    private final int REQUEST_CODE_EDU = 10;//上传学历请求码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_activity);
        ActivityMgr.getActivityMgr().add(this);
        ButterKnife.bind(this);

        mActivity = this;
        mTitle = "设置";

        RelativeLayout ll_title = (RelativeLayout) this.findViewById(R.id.ll_title);
        TextView mTvTitle = (TextView) ll_title.findViewById(R.id.title);
        mTvTitle.setText(mTitle);
        Button btnR = (Button) ll_title.findViewById(R.id.btn_nav_right);
        btnR.setVisibility(View.GONE);
        ImageButton mBtnBack = (ImageButton) ll_title.findViewById(R.id.back);
        mBtnBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mActivity.finish();
            }

        });

        mRlAlertPw = (RelativeLayout) this.findViewById(R.id.rl_alert_pw);
        mHelp = (RelativeLayout) this.findViewById(R.id.rl_help);
        mContact = (RelativeLayout) this.findViewById(R.id.rl_contact);
        mAbout = (RelativeLayout) this.findViewById(R.id.rl_about);
        mFeedback = (RelativeLayout) this.findViewById(R.id.rl_feedback);
        mBtnExit = (Button) this.findViewById(R.id.btn_exit);

        setListener();

        if(AppConfig.eduState ==1||AppConfig.eduState == 2){
            vEdu.setVisibility(View.GONE);
            rlEdu.setVisibility(View.GONE);
        }else{
            vEdu.setVisibility(View.VISIBLE);
            rlEdu.setVisibility(View.VISIBLE);
        }
    }

    private void setListener(){
        mRlAlertPw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity,ChangePasswordActivity.class);
                startActivity(intent);
            }
        });

        rlPrivacy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!AppApplication.networkAvailable) {
                    MyToast.showShort("没有网络");
                    return;
                }

                PrivacySetServiceVisit visit = new PrivacySetServiceVisit(SettingActivity.this);
                visit.setParamters(AppConfig.getInstance().getLoginedUserId());

                Intent intent = new Intent(mActivity,WebActivity.class);
                intent.putExtra("title","隐私设置");
                String userIdEncrypt = AesEncryptionUtil.encrypt(AppConfig.getInstance().getLoginedUserId(),AesEncryptionUtil.Password,AesEncryptionUtil.Iv);
                intent.putExtra("url", ServiceInterfaceDef.SETUP_URL+"?o="+ userIdEncrypt);//MD5Tools.getInstance().getMD5(AppConfig.getInstance().getLoginedUserId()));
                //intent.putExtra("postData",sParamter);
                startActivity(intent);
            }
        });

        rlEdu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity,FurtherInfoSubmitActivity.class);
                startActivityForResult(intent,REQUEST_CODE_EDU);
            }
        });

        mHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mActivity,WebActivity.class);
                intent.putExtra("title","帮助");
                intent.putExtra("url", ServiceInterfaceDef.URL_HELP);
                startActivity(intent);
            }
        });

        mContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity,ContactActivity.class);
                startActivity(intent);
            }
        });

        mAbout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, AboutActivity.class);
                startActivity(intent);
            }
        });

        mFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, YourSuggestionActivity.class);
                startActivity(intent);
            }
        });

        mBtnExit.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                //清空userId
                AppConfig.getInstance().clearLoginedUserId();
                //清空completed
                AppConfig.getInstance().clearCompleted();

                startActivity(new Intent(mActivity, LoginActivity.class));
                ActivityMgr.getActivityMgr().finishAll();
            }

        });
    }


    private void requestPrivacy() {
        progressDialog = CustomProgressDialog.createDialog(this,"正在加载中...");
        progressDialog.show();

        PrivacySetServiceVisit visit = new PrivacySetServiceVisit(this);
        visit.setParamters(AppConfig.getInstance().getLoginedUserId());
        visit.getResult(new NetVisitCallback<PrivacySettingBean,Object>(this){

            @Override
            public void onSuccess(PrivacySettingBean data) {
                if (progressDialog != null && progressDialog.isShowing()) {
                    try {
                        progressDialog.dismiss();
                        progressDialog = null;
                    } catch (Exception e) {
                    }
                }
                if(data!=null){
                    Intent intent = new Intent(mActivity,WebActivity.class);
                    intent.putExtra("title","帮助");
                    intent.putExtra("content", data.getHtmlContent());
                    startActivity(intent);

                }else{

                }

            }

            @Override
            public void onFailure(String errorEvent, String message,Object obj) {
                super.onFailure(errorEvent, message,obj);
                if (progressDialog != null && progressDialog.isShowing()) {
                    try {
                        progressDialog.dismiss();
                        progressDialog = null;
                    } catch (Exception e) {
                    }
                }

                //Toast.makeText(mSelf, ExcepitionMsg.ToastError, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode!=RESULT_OK){
            return;
        }
        switch (requestCode){
            case REQUEST_CODE_EDU:
                //隐藏上传学历
                vEdu.setVisibility(View.GONE);
                rlEdu.setVisibility(View.GONE);
                break;
        }
    }
}
